package com.dzl.admin;

import com.dzl.admin.pojo.SysRole;
import com.dzl.admin.pojo.dto.SaveOrUpdateRoleDto;
import com.dzl.admin.pojo.query.SysRoleQuery;
import com.dzl.admin.pojo.vo.*;
import com.dzl.admin.service.SysRoleService;
import com.dzl.base.controller.BaseController;
import com.dzl.config.exception.CommonException;
import com.dzl.util.EmptyUtils;
import com.dzl.util.JsonResult;
import com.dzl.util.PageUtils;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * 角色表(SysRole)表控制层
 *
 * @author caobucao
 * @since 2021-05-13 15:50:42
 */
@RestController
@RequestMapping("sysRole")
public class SysRoleController extends BaseController {
    /**
     * 服务对象
     */
    @Autowired
    private SysRoleService sysRoleService;

    /**
     * 分页查询角色权限管理列表
     * @date 2021/5/14 10:25
     * @author caobucao
     * @param query 查询条件
     * @return com.dzl.util.PageUtils<com.dzl.role.entity.vo.SysRoleVo>
     */
    @GetMapping("queryRoleListPage")
    public PageUtils<SysRoleVo> queryRoleListPage(SysRoleQuery query) {
        query.setEnterpriseId(getAdminInfo().getEnterpriseId());
        PageInfo<SysRoleVo> pageInfo = sysRoleService.selectRoleListPage(query);
        return new PageUtils<>(pageInfo);
    }

    /**
     * 删除角色
     * @date 2021/5/24 16:27
     * @author caobucao
     * @param id  删除的角色id
     * @return com.dzl.util.JsonResult<java.lang.Integer>
     */
    @DeleteMapping("deleteRoleById")
    public JsonResult<Integer> deleteRoleById(Integer id) {
        SysRole sysRole = new SysRole();
        sysRole.setUpdater(getAdminInfo().getId());
        sysRole.setUpdateTime(new Date());
        sysRole.setStatus(0);
        sysRole.setId(id);
        return JsonResult.success(sysRoleService.deleteRole(sysRole));
    }

    /**
     * 描述:添加角色
     * @date 2021/5/12 10:08
     * @author wangchao
     * @Param [param] 添加角色参数对象
     * @return com.dzl.util.JsonResult
     **/
    @ApiOperation(value = "添加角色", notes = "添加角色")
    @PostMapping("/saveRole")
    public JsonResult<Integer> saveRole(@RequestBody SaveOrUpdateRoleDto param){
        if(EmptyUtils.isEmpty(param)){
            return JsonResult.paramErrWithMsg("请求参数不能为空!");
        }
        if(EmptyUtils.isEmpty(param.getName())){
            return JsonResult.paramErrWithMsg("角色名称不能为空!");
        }
        if(param.getName().equals("超级管理员")) {
            return JsonResult.paramErrWithMsg("不允许添加名为“超级管理员”的角色");
        }
        //获取登录用户
        LoginSysAdminVo adminVo = getAdminInfo();
        if(adminVo!=null){
            param.setCreater(adminVo.getId());
            param.setUpdater(adminVo.getId());
            param.setEnterpriseId(adminVo.getEnterpriseId());
        }
        return JsonResult.success(sysRoleService.saveRole(param));
    }

    /**
     * 描述:修改角色权限
     * @date 2021/5/12 14:11
     * @author wangchao
     * @Param [param]  修改角色参数对象
     * @return com.dzl.util.JsonResult
     **/
    @ApiOperation(value = "修改角色权限", notes = "修改角色权限")
    @PostMapping("/updateRole")
    public JsonResult<Integer> updateRole(@RequestBody SaveOrUpdateRoleDto param){
        if(EmptyUtils.isEmpty(param)){
            return JsonResult.paramErrWithMsg("请求参数不能为空!");
        }
        if(param.getId()==null){
            return JsonResult.paramErrWithMsg("主键参数不能为空!");
        }
        if(EmptyUtils.isEmpty(param.getName())){
            return JsonResult.paramErrWithMsg("角色名称不能为空!");
        }
        //获取登录用户
        LoginSysAdminVo adminVo = getAdminInfo();
        if(adminVo!=null){
            param.setUpdater(adminVo.getId());
        }
        return JsonResult.success(sysRoleService.saveRole(param));
    }

    /**
     * 描述:查询角色详情
     * @date 2021/5/12 11:07
     * @author wangchao
     * @Param [id] 角色ID
     * @return com.dzl.util.JsonResult<com.dzl.role.pojo.vo.RoleEditorVo> 角色色编辑回显对象
     **/
    @ApiOperation(value = "查询角色详情", notes = "查询角色详情")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "角色ID",required = true, defaultValue = "", dataType = "Integer", paramType = "query")
    })
    @GetMapping("/queryRoleById")
    public JsonResult<RoleEditorVo> queryRoleById(Integer id){
        try {
            if(id==null){
                return JsonResult.paramErrWithMsg("参数错误!");
            }
            return JsonResult.success(sysRoleService.queryRoleById(id));
        }catch (Exception e){
            e.printStackTrace();
        }
        return JsonResult.fail();
    }

    /**
     * 描述:根据角色获取菜单树
     * @date 2021/5/12 16:28
     * @author wangchao
     * @Param
     * @return com.dzl.util.JsonResult<java.util.List<com.dzl.menu.pojo.vo.MenuVo>>
     **/
    @ApiOperation(value = "根据角色获取菜单树", notes = "根据角色获取菜单树")
    @GetMapping("/queryByRoleIdMenuTree")
    public JsonResult<List<SysMenuVo>> queryByRoleIdMenuTree(){
        try {
            //获取当前登录用户角色
            LoginSysAdminVo adminVo = getAdminInfo();
            if(adminVo==null){
                throw new CommonException("登录信息已失效");
            }
            Integer isEnable = null;
            //当企业配置为同步平台，则isEnable传1隐藏商品管理等菜单
            if(EmptyUtils.isEmpty(adminVo.getAppRuleConfig())){
                isEnable = 1;
            }
            return JsonResult.success(sysRoleService.queryByRoleIdMenuTree(adminVo.getRoleId(),isEnable));
        }catch (Exception e){
            e.printStackTrace();
        }
        return JsonResult.fail();
    }

    /**
     * 描述:获取角色下拉框
     * @date 2021/5/13 9:03
     * @author wangchao
     * @Param []
     * @return com.dzl.util.JsonResult<java.util.List<com.dzl.role.pojo.vo.RoleComboBoxVo>>
     **/
    @ApiOperation(value = "获取角色下拉框", notes = "获取角色下拉框")
    @GetMapping("/queryRoleComboBox")
    public JsonResult<List<RoleComboBoxVo>> queryRoleComboBox(){
        try {
            return JsonResult.success(sysRoleService.queryRoleComboBox(getAdminInfo().getEnterpriseId()));
        }catch (Exception e){
            e.printStackTrace();
        }
        return JsonResult.fail();
    }

}
